﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace PVFSLEAP
{
	class C_PVFS
	{
		F_SETT ST = new F_SETT();
		C_COMM CM = new C_COMM();

		Random K = new Random( ( int )DateTime.Now.Millisecond );
		Random V = new Random( ( int )DateTime.Now.Millisecond );

		public static SortedList vote = new SortedList();

		public int generateKeys()
		{
			System.Threading.Thread.Sleep( 1 );

			int k = K.Next( 0, ST.OCHLD - 1 );
			return k;
		}

		public bool checkVerifiedNode( int h, int e )
		{
			System.Threading.Thread.Sleep( 1 );

			int v = K.Next( 1, e );

			if ( v <= h )
				return true;
			else
				return false;
		}

		public int getVote()
		{
			System.Threading.Thread.Sleep( 1 );			

			int v = V.Next( 0, ST.OCHLD - 1 );
			return v;
		}

		public void chooseVotes( int chnd, C_NODE[] SN )
		{
			vote.Clear();

			while ( true )
			{
				int v = getVote();
				if ( SN[chnd].CPNF == true )
				{
					bool flag = false;

					for ( int c = 0; c < SN[chnd].CPND.Length; c++ )
					{
						if ( v == SN[chnd].CPND[c] )
						{
							flag = true;
							break;
						}
					}

					if ( vote.Contains( v ) == true ) 
						continue;

					if ( flag == true )
						vote.Add( v, "T" );
					else
						vote.Add( v, "F" );					
				}
				else
				{
					if ( vote.Contains( v ) == true )
						continue;

					vote.Add( v, "F" );
				}

				if ( vote.Count >= ST.OVOTE )
					break;

			}
		}

		public void generateReport( int chnd, C_NODE[] SN )
		{
			chooseVotes( chnd, SN );

			for ( int i = 0; i < SN[chnd].CPND.Length; i++ )
			{
				if ( SN[chnd].CPND[i] != 0 )
					break;
			}

			for ( int i = 0; i < ST.OVOTE; i++ )
				SN[chnd].ENGY = CM.consumeGeneratingVote( SN[chnd].ENGY, 1 );
		}
	}
}
